/*
 * Copyright 2011 SWM Services GmbH.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.swm.commons.mobile.client.widgets;

import com.google.gwt.dom.client.Node;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

import de.swm.commons.mobile.client.SWMMobile;
import de.swm.commons.mobile.client.utils.IsSWMMobileWidgetHelper;
import de.swm.commons.mobile.client.utils.Utils;



/**
 * CheckBox widget.
 * 
 */
public class CheckBox extends com.google.gwt.user.client.ui.CheckBox implements IsSWMMobileWidget {

	private IsSWMMobileWidgetHelper widgetHelper = new IsSWMMobileWidgetHelper();
	private Node imageNode = null;

	/**
	 * Default constructor.
	 */
	public CheckBox() {
		super();
		if (SWMMobile.getOsDetection().isAndroid() && Utils.isWVGA()) {
			CheckBoxIndicator indicator = new CheckBoxIndicator();
			this.getElement().insertFirst(indicator.getElement());
		}
	}

	/**
	 * Sets an image to be displayed as a label for the checkbox.
	 * 
	 * @param imgRes ImageResource for the image to be displayed
	 */
	public void setImage(ImageResource imgRes) {
		Node labelElem = findLabelElement();
		if (labelElem == null) {
			return;
		}
		
		if (imageNode != null) {
			labelElem.removeChild(imageNode);
		}
		
		Image img = new Image(imgRes);
		imageNode = img.getElement();
		labelElem.appendChild(imageNode);
	}


	@Override
	protected void onLoad() {
		super.onLoad();
		widgetHelper.checkInitialLoad(this);
	}



	@Override
	public void setValue(Boolean value) {
		setValue(value, false);
	}



	@Override
	public void setValue(Boolean checked, boolean fireEvents) {
		super.setValue(checked, fireEvents);
		if (checked) {
			addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getCheckRadioBoxCss().selected());
		} else {
			removeStyleName(SWMMobile.getTheme().getMGWTCssBundle().getCheckRadioBoxCss().selected());
		}
	}



	@Override
	public void onInitialLoad() {
	}



	@Override
	public void onTransitionEnd() {
	}



	@Override
	public void setSecondaryStyle(String style) {
		widgetHelper.setSecondaryStyle(this, style);
	}

	/**
	 * Indicator class for Android devices.
	 * 
	 * 
	 *         
	 * 
	 */
	public static class CheckBoxIndicator extends HTML {

		/**
		 * Default constructor.
		 */
		public CheckBoxIndicator() {
			super("<div><div></div></div><div></div><div></div><div></div>");
			setStyleName(SWMMobile.getTheme().getMGWTCssBundle().getCheckRadioBoxCss().checkBoxIndicator());
		}
	}

	private Node findLabelElement() {
		for (Node child = getElement().getFirstChild() ; child != null; child = child.getNextSibling()) {
			if (child.getNodeName().equalsIgnoreCase("label")) {
				return child;
			}
		}
		return null;
	}
}
